{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gurobi_oracle import GurobiSolver\n",
    "from models import LinearModel\n",
    "from sgd import SGDLearner\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr_instances = []\n",
    "test_instances = []\n",
    "\n",
    "for i in range(100):\n",
    "    weights = np.array([np.round(np.random.rand(5)*10,0)])+1\n",
    "    costs = weights + np.random.normal(size=weights.shape)\n",
    "\n",
    "    model_params = {\n",
    "        'A': weights,\n",
    "        'b': np.array([0.5*np.sum(weights)]),\n",
    "        'var_type': 'integer',\n",
    "        'objective': 'max'}\n",
    "    \n",
    "    solver = GurobiSolver()\n",
    "    solver.init_model(model_params)\n",
    "    obj_val, opt_sol = solver.solve(costs)\n",
    "    instance = {\n",
    "        'c': costs,\n",
    "        'features': weights[0],\n",
    "        'A': weights,\n",
    "        'b': np.array([0.5*np.sum(weights)]),\n",
    "        'var_type': 'integer',\n",
    "        'objective': 'max',\n",
    "        'opt_sol': opt_sol,\n",
    "        'opt_val': obj_val\n",
    "    }\n",
    "    if i < 90:\n",
    "        tr_instances.append(instance)\n",
    "    else: \n",
    "        test_instances.append(instance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "avg train loss [3.06121649]\n",
      "best loss [1.83643107]\n"
     ]
    }
   ],
   "source": [
    "learning_params = {\n",
    "    'gradient': 'LS',\n",
    "    'step_size_fn': 'long_dynamic'\n",
    "}\n",
    "learner = SGDLearner(learning_params)\n",
    "oracle = GurobiSolver()\n",
    "model_spoplus = learner.learn(oracle, LinearModel(5, 5), \n",
    "              tr_instances, test_instances,\n",
    "              batch_size = 90, epochs=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "avg train loss [2.61952594]\n",
      "best loss [3.01398069]\n"
     ]
    }
   ],
   "source": [
    "learning_params = {\n",
    "    'gradient': 'spo_plus',\n",
    "    'step_size_fn': 'long_dynamic'\n",
    "}\n",
    "learner = SGDLearner(learning_params)\n",
    "oracle = GurobiSolver()\n",
    "model_spoplus = learner.learn(oracle, LinearModel(5, 5), \n",
    "              tr_instances, test_instances,\n",
    "              batch_size = 90, epochs=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learning_params = {\n",
    "    'gradient': 'decision_loss_mse',\n",
    "    'step_size_fn': 'long_dynamic'\n",
    "}\n",
    "learner = SGDLearner(learning_params)\n",
    "oracle = GurobiSolver()\n",
    "model_spoplus = learner.learn(oracle, LinearModel(5, 5), \n",
    "              tr_instances, test_instances,\n",
    "              batch_size = 90, epochs=1000)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
